﻿{extend name="common/base"}
{block name="title"}购物车页{/block}
<body>
{block name="content"}
	<!-- cart -->
	<div class="segments-page">
		<div class="container">
			<div class="pages-title">
				<h3>购物车</h3>
			</div>
			<form action="{:url('index/cart/checkoutCar')}" method="POST" id="car-form">
			<div class="cart">
				{if ($row!=NULL)}
				{foreach name="row" id="ro"}
				<div class="cart-product first" id="car2{$ro.car_id}">
					<div class="row">
						<div class="col s4">
							<div class="contents">
								<img src="{$ro['src'][0]}" alt="">
							</div>
						</div>
						<div class="col s6">
							<div class="contents">
								<a href="{:Url('product/product_details')}?id={$ro.id}">{$ro.name}</a>
							</div>
						</div>
						<div class="col s6">
							<div class="contents">
								<p>{$ro.first_guige_name} {$ro.guigeName}</p>
							</div>
						</div>
						<div class="col s2">
							<div class="contents remove">
								<i class="fa fa-remove" onclick="showModal(this);" style="cursor:pointer;">
									<input type="hidden" value="{$ro.car_id}">
								</i>
							</div>
						</div>
					</div>
					<div class="row">
						<div class="col s4">
							<div class="contents">
								<p>价格</p>
							</div>
						</div>
						<div class="col s8">
							<div class="contents">
								<p class="price" id="gPrice{$ro.car_id}">¥{$ro.price}</p>
								<input type="hidden" id="priceBox{$ro.car_id}" value="">
							</div>
						</div>
						<div class="col s4">
							<div class="contents">
								<p>数量</p>
							</div>
						</div>
						<div class="col s6">
							<div class="contents">
								<input type="number" name="num[]" value="{$ro.num}" id="gNum{$ro.car_id}" onchange="getTPrice(this)">
							</div>
						</div>
					</div>
				</div>
				{/foreach}
				{/if}
			</div>
			<div class="total-pay">
				<div class="row">
					{if ($row!=NULL)}
					{foreach name="row" id="ro"}	
					<div class="pBox" id="pBox{$ro.car_id}">
						<div class="col s8">
							<div class="contents">
								<p>{$ro.name}</p>
							</div>
						</div>
						<div class="col s4">
							<div class="contents right">
								<p id="goodsPrice{$ro.car_id}" class="tPrice">¥{$ro.price}</p>
								<input type="hidden" id="xiaoji{$ro.car_id}" name="xiaoji[]" value="{$ro.price}">
							</div>
						</div>
					</div>
					{/foreach}
					{/if}
					<div class="col s8">
						<div class="contents">
							<h5>总计</h5>
						</div>
					</div>
					<div class="col s4">
						<div class="contents right">
							{if ($totalPrice!=NULL)}
							<h5 id="totalPrice">¥{$totalPrice}</h5>
							<input type="hidden" id="zongji" name="zongji" value="{$totalPrice}">
							{else /}
							<h5 id="totalPrice">¥0</h5>
							{/if}
						</div>
					</div>
				</div>
				<a href="" onclick="document.getElementById('car-form').submit();return false"><button class="button"><i class="fa fa-send"></i>去结账</button></a>
			</div>
		</form>
		</div>
	</div>
	<!-- end cart -->
{/block}

{block name="footjs"}
<script>
	//计算总价
	function getTPrice(obj){
		//获取购物车id
		var carid = $(obj).attr('id').substring(4);
		// alert(carid);
		//计算每个产品的价格
		var strPrice = $("#gPrice"+carid).html();
		var Price = strPrice.substring(1);
		$("#priceBox"+carid).val(Price);
		$("#goodsPrice"+carid).html('￥'+$("#gNum"+carid).val()*$("#priceBox"+carid).val());
		$("#xiaoji"+carid).val($("#gNum"+carid).val()*$("#priceBox"+carid).val());
		// alert(Price);
		//获取多个产品总价的长度
		var length = $(".total-pay").find('.pBox').length;
		//计算总价
		var totalPrice = 0;
		for(var i=0;i<length;i++){
			totalPrice += parseInt($('.tPrice').eq(i).html().substring(1));
		}
		$("#totalPrice").html('￥'+totalPrice);
		$("#zongji").val(totalPrice);
	}
</script>
{/block}
